package com.zut.dao.impl;

import com.zut.dao.ScoreDao;
import com.zut.util.JdbcUtil;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

/**
 * 成绩的持久层实现类
 */
public class ScoreDaoImpl implements ScoreDao {

    //工具对象
    private JdbcTemplate template = JdbcUtil.template;

    /**
     * 添加成绩
     * @param stuId
     * @param id
     * @param grade
     */
    @Override
    public void addScore(String stuId, int id, String grade) {

        String sql = "insert into score values(null,?,?,?)";
        template.update(sql,grade,id,stuId);
    }


    /**
     * 根据教师id 和 课程id 进行成绩查询
     * @param tid
     * @param id
     * @return
     */
    @Override
    public List<Map<String, Object>> findScoresByTidAndCid(String tid, int id) {
        String sql = "select st.id,st.name,score.grade,score.cid from (select * from student where classId in (select classId from teachingclass where tid = ?)) st left join score on st.id = score.sid and score.cid = ?";

        return template.queryForList(sql,tid,id);
    }


    /**
     * 修改学生成绩
     *
     * @param stuId
     * @param id
     * @param newGrade
     * @return
     */
    @Override
    public int updateGrade(String stuId, int id, String newGrade) {
        String sql = "update score set grade=? where sid=? and cid=?";
        int i = template.update(sql,newGrade,stuId,id);
        return i;
    }

    /**
     * 根据学号和课程id 删除成绩
     * @param stuId
     * @param id
     * @return
     */
    @Override
    public int deleteScore(String stuId, int id) {
        return template.update("delete from score where sid=? and cid=?",stuId,id);
    }

    /**
     * 根据学生id查询学生各科成绩
     * @param id
     * @return
     */
    @Override
    public List<Map<String, Object>> findScoresBySid(String id) {
        String sql = "select course.cname,score.grade from course left join score on course.cno = score.cid where score.sid=?";
        return template.queryForList(sql,id);
    }
}
